var API = require('API').survey.temp;
var ORDER_API = require('API').survey.order;
var QUESTION_API = require('API').survey.qa;
var GRID = require('temp.aggrid');
var DateUtil = require('DateUtil');

module.exports = ['$scope', '$rootScope', '$location', '$routeParams', '$timeout',
    function ($scope, $rootScope, $location, $routeParams, $timeout) {

        $scope.add_open = false;
        $scope.edit_open = false;
        $scope.grid = GRID.grid;

        $scope.record_add = {};
        $scope.record_edit = {};

        $scope.question_open = false;

        $scope.submit_add = function () {

            var param={
                name:$scope.record_add.name
            };

            API.add_temp(param, function (json) {
                //添加题目
                var question=$scope.record_add.question;
                if(question&&question.length!=0){
                    var total=question.length;
                    $.each(question,function(i,v){
                        v.sn=i+1;
                        API.add_detail(json.id,v, function () {
                            if(i+1==total){
                                $scope.$apply(function () {
                                    $scope.refresh();
                                });
                            }
                        }, function (e) {
                            alert(e)
                        });

                    });
                }

            }, function (e) {
                alert(e);
            });
        };

        $scope.open_edit = function (data) {
            //$scope.edit_open = true;
            //$scope.record_edit = data;
            window.open("/customer_service/temp_display/"+data.id);
        };

        GRID.on('open', function (row) {
            $scope.$apply(function () {
                $scope.open_edit(row);
            });
        });

        GRID.on('del', function (row) {
            //
            API.del(row.id, function () {
                $scope.$apply(function () {
                    $scope.refresh()
                });
            }, function (e) {
                alert(e);
            });
        });

        GRID.on('export', function (row) {
                ORDER_API.build_by_tmpl(row.id,function(json){
                    window.open("/customer_service/order_display/"+json.id);
                });

        });

        $scope.submit_edit = function () {
            //
            API.upd($scope.record_edit, function () {
                $scope.$apply(function () {
                    $scope.refresh();
                });
            }, function (e) {
                alert(e);
            });
        };

        $scope.refresh = function () {
            GRID.refresh();
            $scope.add_open = false;
            $scope.edit_open = false;

            $scope.record_add = {};
            $scope.record_edit = {};
        }



        var question_cols = [
            {
                field: '',
                headerName: '序号',
                filterParams: {newRowsAction: 'keep'},
                cellRenderer: function (p) {
                    return p.rowIndex+1;
                }
            },
            {
                field: 'content',
                headerName: '题目',
                filterParams: {newRowsAction: 'keep'}
            },


            {
                field: 'create_time',
                headerName: '创建时间',
                filterParams: {newRowsAction: 'keep'},
                cellRenderer: function (p) {
                    if (p.value) return DateUtil.format(p.value);
                    return "";
                }
            }
        ];

        var question_getData = function () {

            return {
                getRows: function (p) {

                    QUESTION_API.search_question({}, function (json, status, xhr) {
                        var count = xhr.getResponseHeader('X-Total-Count');
                        p.successCallback(json, Number(count));
                    });
                },
                pageSize: 50

            }
        };

        $scope.question_kpi = {
            columnDefs: question_cols,
            rowData: [],
            datasource: question_getData(),
            rowModelType: 'pagination',
            enableServerSideSorting: true,
            enableServerSideFilter: true,
            suppressCellSelection: true,
            rowHeight: 22,
            colWidth: 100,
            enableColResize: true,
            enableCellExpressions: true,
            api: {},
            columnApi: {},
            rowSelection: 'single',
            localeText: require('locale').aggrid
        };

        $scope.choose_question = function () {
            $scope.question_kpi.api.setDatasource(question_getData());
            $scope.question_open = true;
        }

        $scope.question_kpi.onRowClicked = function (p) {

            $scope.$apply(function () {
                if(!$scope.record_add.question){
                    $scope.record_add.question=[];
                }
                $scope.record_add.question.push({
                    question_id:p.data.id,
                    content: p.data.content
                });
                $scope.question_open = false;
            });
        };

    }];